var Dialogs = {
    lightColorFlag : "_light.",

    /**
     * The method is used by editQuestions.jsp and viewQuestions.jsp.
     */
    subItemClick : function (item, params, btnGroupName) {
        var btns = document.getElementsByName(btnGroupName);

        if ($(item).attr("class").indexOf("active") == -1) {
            // Remove the active class for other items. (editQuestions.jsp)
            $("div.question table").removeClass("active");
            // Remove the active class for other items. (whereUsed.jsp)
            $("#dataList div").removeClass("active");

            $(item).addClass("active");

            itemParams = params;
            for (i = 0; i < btns.length; i++) {
                var btn = btns[i];

                // Enable the button.
                $(btn).removeAttr("disabled");

                var imgSrc = $(btn).attr("src");
                var newSrc = imgSrc.replace(lightColorFlag, '.');
                $(btn).attr("src", newSrc);
            }

            if ($(item).attr("status") == 'retired') {
                this.inactiveBtn($("#editQuestionBtn"), lightColorFlag);
            }

            if ($(item).attr("availability") == 'locked') {
                this.inactiveBtn($("#editQuestionBtn"), lightColorFlag);
            }

            // This block of code is used by whereUsed.jsp in 'Page' Module.
            if ($(item).attr("pstatus") != 'inprocess') {
                this.inactiveBtn($("#editDialogBtn"), lightColorFlag);
            }
        } else {
            $(item).removeClass("active");

            itemParams = "";

            for (i = 0; i < btns.length; i++) {
                var btn = btns[i];

                $(btn).removeClass("btn");

                // Disable the button.
                $(btn).attr("disabled", true);

                var imgSrc = $(btn).attr("src");
                if (imgSrc.indexOf(lightColorFlag) == -1)
                    imgSrc = imgSrc.replace(".", lightColorFlag);
                $(btn).attr("src", imgSrc);
            }
        }
    },

    inactiveBtn : function (btn, inactiveImgFlag) {
        if (btn.length > 0) {
            var imgSrc = btn.attr("src");
            if (imgSrc.indexOf(inactiveImgFlag) == -1)
                if (imgSrc.indexOf("_on.gif") != -1) {
                    imgSrc = imgSrc.replace("_on.gif", inactiveImgFlag + "gif");
                } else {
                    imgSrc = imgSrc.replace(".gif", inactiveImgFlag + "gif");
                }
            btn.attr("src", imgSrc);
            btn.attr("disabled", true);
        }
    },

    activeBtn : function (btn, inactiveImgFlag) {
        if (btn.length > 0) {
            btn.removeAttr("disabled");
            var imgSrc = btn.attr("src");
            imgSrc = imgSrc.replace(inactiveImgFlag, ".");
            btn.attr("src", imgSrc);
        }
    },

    unescapedLink : function (originalLink, unescapedParam) {
        var fromUrl = this.getParamFromUrl(unescapedParam);
        if (fromUrl != null && fromUrl != '') {
            if (originalLink.indexOf('?') != -1) {
                window.location.href = originalLink + "&from=" + fromUrl;
            } else {
                window.location.href = originalLink + "?from=" + fromUrl;
            }
        } else {
            window.location.href = originalLink;
        }
    },

    getParamFromUrl : function (paramName) {
        var url = document.location.href;

        if (url.indexOf(paramName) == -1) {
            return "";
        }
        var begin = url.indexOf(paramName) + paramName.length + 1;
        var end = url.length;
        return url.substring(begin, end);
    },

    /**
     * This method is used to put the target info data the "from" field. This can make the app can make the target data
     * across the redirect.
     */
    saveTargetValue : function () {
        $("#from").val(this.getParamFromUrl('from'));
    }
}

$.fn.enableContentChangedConfirm = function () {
    var _f = this;

    $(':text, :password, textarea', this).each(function() {
        $(this).attr('_value', $(this).val());
    });

    $(':checkbox, :radio', this).each(function() {
        var _v = this.checked ? 'on' : 'off';
        $(this).attr('_value', _v);
    });

    $('select', this).each(function() {
        $(this).attr('_value', this.options[this.selectedIndex].value);
    });

    $(this).each(function () {
        if ($(this).attr('eccc') == 'true')
            $(this).attr('_value', $(this).html());
    });

    $(this).submit(function() {
        window.onbeforeunload = null;
    });
    window.onbeforeunload = function() {
        if (isFormChanged(_f)) {
            return "You will lose any unsaved content.";
        }
    }
}

function isFormChanged(f) {
    var changed = false;

    $(':text, :password, textarea', f).each(function() {
        var _v = $(this).attr('_value');
        if (typeof(_v) == 'undefined')   _v = '';
        if (_v != $(this).val()) changed = true;
    });

    $(':checkbox, :radio', f).each(function() {
        var _v = this.checked ? 'on' : 'off';
        if (_v != $(this).attr('_value')) changed = true;
    });

    $('select', f).each(function() {
        var _v = $(this).attr('_value');
        if (typeof(_v) == 'undefined')
            _v = '';
        if (_v != this.options[this.selectedIndex].value) changed = true;
    });

    $(f).each(function () {
        if ($(this).attr('eccc') == 'true')
            if ($(this).html() != $(this).attr('_value'))
                changed = true;
    });

    return changed;
}